#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initSockSingnals(&systemSock);
    initSockSingnals(&commandSock);
    initSockSingnals(&videoSock);
    initSockSingnals(&audioSock);
    init();
    connectAll();
    connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(send()));
    f = new QFile("c:\\piz.dat");
}

MainWindow::~MainWindow()
{
    disconnectAll();
    delete ui;
}

void MainWindow::init()
{
    systemSock.setSockType(stSystem);
    commandSock.setSockType(stCommand);
    videoSock.setSockType(stVideo);
    audioSock.setSockType(stAudio);
    strcpy(sInfo[stSystem].name,"system");
    strcpy(sInfo[stCommand].name,"command");
    strcpy(sInfo[stVideo].name,"video");
    strcpy(sInfo[stAudio].name,"audio");
}

void MainWindow::initSockSingnals(QTcpSocket *sock)
{
    connect(sock,SIGNAL(connected()),this,SLOT(connected()));
    connect(sock,SIGNAL(disconnected()),this,SLOT(disconnected()));
    connect(sock,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error(QAbstractSocket::SocketError)));
    connect(sock,SIGNAL(readyRead()),this,SLOT(readInit()));
}

void MainWindow::connectAll()
{
    systemSock.connectToHost("127.0.0.1",6669);
    commandSock.connectToHost("127.0.0.1",6669);
    videoSock.connectToHost("127.0.0.1",6669);
    audioSock.connectToHost("127.0.0.1",6669);
}

void MainWindow::disconnectAll()
{
    if(systemSock.isOpen())systemSock.close();
    if(commandSock.isOpen())commandSock.close();
    if(videoSock.isOpen())videoSock.close();
    if(audioSock.isOpen())audioSock.close();
}

void MainWindow::connected()
{
    NTcpSocket *sock = (NTcpSocket *)sender();
    ui->logEdit->insertPlainText("<"+QString(sInfo[sock->getSockType()].name)+">: connected to host\n");
    char buf = (char) sock->getSockType();
    sock->write(&buf,1);
}

void MainWindow::disconnected()
{
    NTcpSocket *sock = (NTcpSocket *)sender();
    if(sock->getSockType()==stVideo)
        f->close();
    ui->logEdit->insertPlainText("<"+QString(sInfo[sock->getSockType()].name)+">: disconnected from host\n");
}

void MainWindow::error(QAbstractSocket::SocketError error)
{
    NTcpSocket *sock = (NTcpSocket *)sender();
    ui->logEdit->insertPlainText("<"+QString(sInfo[sock->getSockType()].name)+">: "+sock->errorString()+"\n");
}

void MainWindow::send()
{
    systemSock.write(ui->commandLine->text().toAscii());
    ui->logEdit->insertPlainText("<< "+ui->commandLine->text()+"\n");
}

void MainWindow::readInit()
{
    NTcpSocket *sock = (NTcpSocket *)sender();
    char buf;
    sock->read(&buf,1);
    ui->logEdit->insertPlainText("<"+QString(sInfo[sock->getSockType()].name)+">>");
    if((commonReplays)buf != crOk)
    {
        ui->logEdit->insertPlainText("error initialization\n");
       return;
    }
    disconnect(sock,SIGNAL(readyRead()),this,SLOT(readInit()));
    ui->logEdit->insertPlainText("initialized\n");
    switch((sockTypes)sock->getSockType())
    {
        case stCommand:
            connect(sock,SIGNAL(readyRead()),this,SLOT(readCommand()));
            break;
        case stSystem:
            connect(sock,SIGNAL(readyRead()),this,SLOT(readSystem()));
            break;
        case stAudio:
            connect(sock,SIGNAL(readyRead()),this,SLOT(readAudio()));
            break;
        case stVideo:
            f->open(QIODevice::WriteOnly);
            ds = new QDataStream(f);
            connect(sock,SIGNAL(readyRead()),this,SLOT(readVideo()));
            break;
    }
}

void MainWindow::readAudio()
{
    while(!audioSock.atEnd())
    {
        ui->logEdit->insertPlainText("<audio>> "+audioSock.readLine());
    }
}

void MainWindow::readCommand()
{
    while(!commandSock.atEnd())
    {
        ui->logEdit->insertPlainText("<command>> "+commandSock.readLine());
    }
}

void MainWindow::readSystem()
{
    while(!systemSock.atEnd())
    {
        ui->logEdit->insertPlainText("<system>> "+systemSock.readLine());
    }
}

void MainWindow::readVideo()
{
    ui->logEdit->insertPlainText("<video> get message\n");
    char buf[1024];
    QByteArray ba(buf,1024);
    int readBytes;
    while(!videoSock.atEnd())
    {
        readBytes = videoSock.read(buf,1024);
        ds->writeRawData(buf,readBytes);
    }
}
